﻿using AForge_zxing;
using Supermarket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XLSupermarket.Storehouse.Commodity_Delivery
{
    public partial class Commodity_Delivery : Form
    {
        public Commodity_Delivery()
        {
            InitializeComponent();
        }

        private void Commodity_Delivery_Load(object sender, EventArgs e)
        {

        }

     
       /// <summary>
       /// 商品出库
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int Delivery_Number = int.Parse(this.textBox3.Text);
            string Delivery_time = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string Other = this.textBox5.Text;
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("没输入商品条形码");
            }
            else
            {
                if (this.textBox3.Text == "")
                {
                    MessageBox.Show("没输入商品数量");
                }
                else
                {
                    string Bar_code = this.textBox2.Text;
                    string sql1 = string.Format("select Stock_Number 库存编号,Inventory_quantity 库存数量 from Merchandise_inventory where Bar_code = '{0}'", Bar_code);
                    DataTable dt1 = DBHelper.FillData(sql1);
                    string Stock_Number = dt1.Rows[0][0].ToString();
                    int num1 = int.Parse(dt1.Rows[0][1].ToString());
                    if (Delivery_Number > num1)
                    {
                        MessageBox.Show("商品库存不足,库存剩余" + num1 + "，请重新输入商品数量");
                    }
                    else
                    {
                        //插入商品库存
                        string sql2 = string.Format("insert into Delivery_goods values('{0}','{1}','{2}','{3}')", Stock_Number, Delivery_Number, Delivery_time, Other);
                        int num2 = DBHelper.Edit(sql2);
                        sql2 = "select * from Merchandise_inventory where bar_code = " + textBox2.Text;
                        DataTable dt_2 = DBHelper.FillData(sql2);
                        string vipjg = dt_2.Rows[0]["Membership_Price"].ToString();//会员价
                        string danjia = dt_2.Rows[0]["Retail_Price"].ToString();//单价
                        string com ="本";//规格
                        string num = textBox3.Text;//个数
                        string bar = textBox2.Text;//tiaoma



                        sql2 = string.Format("insert into Goods_Shelves Values('{0}','{1}','{2}','{3}','{4}')",num,com,bar,danjia,vipjg                            );
                        int aa = DBHelper.Edit(sql2);
                        
                        if (num2 > 0)
                        {

                            string sql3 = string.Format("update Merchandise_inventory set Inventory_quantity = Inventory_quantity-{0} where Bar_code='{1}'", Delivery_Number, Bar_code);
                            int num3 = DBHelper.Edit(sql3);
                            if (num3 > 0)
                            {
                                MessageBox.Show("出库成功");
                                Clear();
                            }
                        }
                        else
                        {
                            MessageBox.Show("出库失败");
                        }
                    }
                }
            }
            //存库存数量
            //判断出库数量多少
            //在商品出库中新增库存数量，同时在商品库存表中减少相应数量
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string Bar_code = this.textBox2.Text;
                string sql = string.Format("select * from Merchandise_inventory MI,Goods_Warehousing GW,Goods_Type GT,Supplier SL where MI.Bar_code=GW.Bar_code and GT.GoodsType_ID=GW.GoodsType_ID and SL.Supplier_ID=MI.Supplier_ID and GW.Bar_code='{0}'", Bar_code);
                DataTable dt = DBHelper.FillData(sql);
                this.label10.Text = dt.Rows[0]["Trade_Name"].ToString();
                this.label11.Text = dt.Rows[0]["GoodsType_Name"].ToString();
                this.label12.Text = dt.Rows[0]["Supplier_Name"].ToString();
            }
            catch (Exception)
            {

            }
        }
        private void Clear()
        {
            this.textBox2.Text = null;
            this.textBox3.Text = null;
            this.textBox5.Text = null;
            this.label10.Text = null;
            this.label11.Text = null;
            this.label12.Text = null;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            Form1 AF = new Form1();
            DialogResult result = AF.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.textBox2.Text = AF.reisbn;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Commodity_Delivery_Table CDT = new Commodity_Delivery_Table();
            CDT.ShowDialog();
        }
    }
}
